#include "managerview_impl.h"
#include<iomanip>
ManagerViewImpl::ManagerViewImpl()
{
	manager = new ManagerImpl;
}
void ManagerViewImpl::Menu(void)
{
	cout << "*******欢迎来到图书管理系统*******" << endl;
	cout << "        1、添加书籍               " << endl;
	cout << "        2、借书    	           " << endl;
	cout << "        3、还书		           " << endl;
	cout << "        4、显示书籍	           " << endl;
	cout << "        5、添加读者               " << endl;
	cout << "        6、删除读者               " << endl;
	cout << "        7、显示读者               " << endl;
	cout << "        8、充值	               " << endl;
	cout << "        9、退出 		           " << endl;
	cout << "**********************************" << endl;
}
void ManagerViewImpl::Run(void)
{
	while(1)
	{
	    system("clear");
		Menu();
		cout << "请输入指令：";
		char cmd = 0;
		while(1)
		{
			cmd = getch();
			if(cmd >='1' && cmd <='9')
				break;
		}
		cout << cmd << endl;
		switch(cmd-'0')
		{
			//添加书籍
			case 1:AddBook(); break;
			//借书
			case 2:BorrowBook(); break;
			//换书
			case 3:ReturnBook(); break;
			//显示书籍
			case 4:ShowBook(); break;
			//添加读者
			case 5:AddReader(); break;
			//删除读者
			case 6:DelReader(); break;
			//显示读者
			case 7:ShowReader(); break;
			//充值
			case 8:SetMoney(); break;
			//推出
			case 9:manager->save();return;
		}
	}
}

void ManagerViewImpl::SetMoney(void)
{
	int id = 0;
	cout << "请输入读者ID:";
	cin >> id;
	int cash = 0;
	cout << "请输入充值的金额:";
	cin >> cash;
	int money = manager->SetMoney(cash,id);
	if(money == 0)
	{
		cout << "没有此人" << endl;
	}
	else
		cout << "您当前的余额：" << money << endl;
	anykey_continue();

}
void ManagerViewImpl::AddBook(void)
{
	char name[20] = {};
	cout << "请输入书名：";
	cin >> name;
	int id = generator_BookId();
	Book book(name,id);
	if(manager->AddBook(book))
	{
        cout << "添加书籍成功！" << endl;
    }
    else
    {
        cout << "添加书籍失败！" << endl;
    }
	anykey_continue();
}
void ManagerViewImpl::AddReader(void)
{
	char name1[20] = {};
	cout << "输入要添加用户的姓名：";
	cin >> name1;
	int id1 = generator_ReaderId();
	int money1 = 0;
	cout << "输入定金：";
	cin >> money1;
	Reader r(name1,id1,money1);
	manager->AddReader(r);
	anykey_continue();
}
void ManagerViewImpl::BorrowBook(void)
{
	int u_id = 0;
	cout << "请输入用户ID：";
	cin >> u_id;
	int b_id = 0;
	cout << "请输入书名的ID：";
	cin >> b_id;
	if(manager->BorrowBook(b_id,u_id))
		cout << "借书成功" << endl;
	anykey_continue();
}
void ManagerViewImpl::ReturnBook(void)
{
	int u_id = 0;
	cout << "请输入用户ID：";
	cin >> u_id;
	int b_id = 0;
	cout << "请输入书名的ID：";
	cin >> b_id;
	manager->ReturnBook(b_id,u_id);
	anykey_continue();
}
void ManagerViewImpl::DelReader(void)
{
	manager->DelReader();
	anykey_continue();
}
void ManagerViewImpl::ShowBook(void)
{
	manager->ShowBook();
	anykey_continue();
}
void ManagerViewImpl::ShowReader(void)
{
	manager->ShowReader();
	anykey_continue();
}
